define(['app', 'assets/js/bootstrap.bundle.min.js', 'assets/js/bootstrap-select.js'], function (app) {
    return {
        module: function ($scope, $http, eventBusService, httpService, params) {
            $scope.isClassMajorShow = app.parameter.parameter_isClassMajorShow;
			$scope.form.fk_course = params.id;
            $scope.form.id = params.id;

            //学段列表
            httpService.post(app.urls.stage_selectAll, {}, function (data) {
                if ("0000" === data.code) {
                    $scope.stageList = data.data;
                } else {
                    alert(data.msg);
                }
            });

            //查询主校区和分校区
            httpService.post(app.urls.unit_selectSchoolAndAttach, {}, function (data) {
                if ("0000" === data.code) {
                    $scope.unitList = data.data;
                } else {
                    alert(data.msg);
                }
            });
            httpService.post(app.urls.course_selectById, $scope.form, function (data) {
                if ("0000" === data.code) {
                    $scope.form = data.data;
                } else {
                    alert(data.msg);
                }
            });
            $scope.saveForm = function () {
                if ($scope.formCheck()) {
                    if ($scope.form.flag == 1) {
                        $scope.form.fk_unit = undefined;
                    }
                    if ($scope.form.flag == 2 && $scope.form.fk_unit == undefined) {
                        alert("特色课程，学校不能为空");
                        return;
                    }
                    httpService.post(app.urls.course_selectByName, $scope.form, function (data) {
                        if ("0000" === data.code) {
                            if (data.data.length == 0) {
                                httpService.post(app.urls.course_update, $scope.form, function (data) {
                                    if ("0000" === data.code) {
										if($scope.isClassMajorShow === "1"){
											$scope.form.fk_course = $scope.form.id;
											$scope.form.fk_major = $("#major_select").val();
											$scope.form.fk_organization = $("#office_select").val();
											httpService.post(app.urls.course_other_select, $scope.form, function (data) {
												if ("0000" === data.code) {
													if (data.data.length>0) {
														httpService.post(app.urls.course_other_update, $scope.form, function (data) {});
													}else{
														httpService.post(app.urls.course_other_insert, $scope.form, function (data) {});
													}
												}
											});
										}
                                        eventBusService.publish("content.page.reload", data.data);
                                        eventBusService.publish("modal.page.close", {});
                                    } else {
                                        alert(data.msg);
                                    }
                                });
                            } else {
                                alert("该课程已存在");
                            }
                        } else {
                            alert(data.msg);
                        }
                    });
                }
            }

			$scope.findCourseOther = function (){
				httpService.post(app.urls.course_other_select,{'fk_course':$scope.form.id},function(data){
					if ("0000" === data.code) {
						let result = data.data[0];
						$scope.form = {...$scope.form, ...result}
						setTimeout(function (){
							$('#major_select').selectpicker('val', $scope.form.fk_major+"");
							$('#office_select').selectpicker('val', $scope.form.fk_organization+"");
						},200)
						$scope.$applyAsync();
					}
				})
			}

			$scope.findMajorGroups = function (){
				httpService.post(app.urls.findMajorGroups,{},function(data){
					if("0000" === data.code){
						$scope.majorGradeList = data.data;
						let majorGradeList = data.data;
						$('#major_select').html("");
						let optHtml = '<option value="">==请选择==</option>';
						// let optHtml = '';
						for (i in majorGradeList){
							let gradeName = majorGradeList[i].grade_name;
							let major = majorGradeList[i].major;
							optHtml += '<optgroup label='+gradeName+'>';
							for(a in major){
								optHtml += '<option data-subtext='+major[a].code+' value='+major[a].major_id+'>' + major[a].major_name+ '</option>'
							}
							optHtml += '</optgroup>'
						}
						$("#major_select").append(optHtml);
						$("#major_select").selectpicker('refresh');
						$("#major_select").selectpicker('render');
					}else{
						alert(data.msg);
					}
				});
			}

			$scope.findOffice = function (){
				httpService.post(app.urls.findOffice,{},function(data){
					if("0000" === data.code){
						$scope.officeList = data.data;
					}else{
						alert(data.msg);
					}
				});
			}

			let init = function () {
				if($scope.isClassMajorShow === "1"){
					$scope.findCourseOther();
					$scope.findMajorGroups();
					$scope.findOffice();
					setTimeout(function (){
						$('#major_select').selectpicker({
							'noneSelectedText': '==请选择==',
							'title': '==请选择==',
							'liveSearch': true,
							'noneResultsText': '没有匹配到查询结果：{0}',
							'header': '选择专业',
							'showSubtext': true,
							'size': 10
						});
						$('#office_select').selectpicker({
							'noneSelectedText': '==请选择==',
							'title': '==请选择==',
							'liveSearch': true,
							'noneResultsText': '没有匹配到查询结果：{0}',
							'header': '选择教研室',
							'size': 10
						});
					}, 100);
				}
			}

			init();
        }
    }
});